<?php

namespace App\Http\Requests;


use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Validator;

class Checkpwd extends FormRequest
{
    public function __construct( )
    {
        /*自动注册检测旧密码方法*/
        Validator::extend('checkpwd', function ($attribute, $value, $parameters, $validator) {
            return Hash::check($value, Auth::user()->password);
        });
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }





    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {

        return [
            'oldpwd' => 'required|checkpwd',
            'newpwd' => 'required|confirmed',
            'newpwd_confirmation' => 'required'

        ];
    }


    //返回错误信息
    public function messages()
    {
        return [
            'oldpwd.required' => '旧密码不能为空',
            'oldpwd.checkpwd' => '旧密码不对',
            'newpwd.required' => '新密码不能为空',
            'newpwd_confirmation.required' => '新密码不能为空',
            'newpwd.confirmed' => '两次输的新密码不一致',
        ];
    }
}
